# Created with pycharm
# author:ju bai
# Date:2021/7/21
# Time:8:16
from selenium import webdriver
import unittest
import time
from ddt import ddt, unpack, data, file_data
import sys, csv


# 文本格式化方法
def getCsv(file_name):
    rows = []
    path = sys.path[0]

    with open(path + '/data/' + file_name, 'rt') as f:
        readers = csv.reader(f, delimiter=',', quotechar='|')
        next(readers, None)
        for row in readers:
            temprows = []
            for i in row:
                # 读每一行的数据
                temprows.append(i)
            rows.append(temprows)
        return rows


@ddt
class Baidu(unittest.TestCase):
    # 测试固件
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(30)
        self.base_url = "http://www.baidu.com/"
        self.driver.maximize_window()
        self.verificationErrors = []
        self.accept_next_alert = True

    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

    # @data("姜云升", "娱乐", "新闻", "体育")
    # @file_data('test_baidu_data.json')
    # @data(["自白书", "自白书_百度搜索"], ["体育", "体育_百度搜索"], ["新闻", "新闻_百度搜索"])
    @data(*getCsv('test_baidu_data.txt'))
    @unpack
    def test_baidu1(self, value):
        driver = self.driver
        driver.get(self.base_url + "/")
        driver.maximize_window()
        driver.find_element_by_id("kw").clear()
        driver.find_element_by_id("kw").send_keys(value)
        driver.find_element_by_id("su").click()
        # time.sleep(6)
        # self.assertEqual(driver.title, expected_value, msg="搜索结果和期望不一致!")
        time.sleep(3)

    if __name__ == "__main__":
        unittest.main(verbosity=0)
